package hu.bme.aut.consulttime.server.email;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailProvider {
	
	   public static void sendEmail(String toAddress, String subject, String body) throws MessagingException {

	        Properties properties = new Properties();

	        properties.setProperty("mail.smtp.host", "smtp.gmail.com");
	        properties.setProperty("mail.smtp.auth", "true");
	        properties.setProperty("mail.smtp.port", "587");
	        properties.setProperty("mail.smtp.starttls.enable", "true");

	        //Here, no Authenticator argument is used (it is null).
	        //Authenticators are used to prompt the user for user
	        //name and password.
	        Session session = Session.getDefaultInstance(properties,
	                new javax.mail.Authenticator() {

	                    @Override
	                    protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
	                        return new javax.mail.PasswordAuthentication("usernamegeorge@gmail.com", "invarion");
	                    }
	                });
	        MimeMessage message = new MimeMessage(session);
	        //the "from" address may be set in code, or set in the
	        //config file under "mail.from" ; here, the latter style is used
	        //message.setFrom( new InternetAddress(aFromEmailAddr) );
	        message.addRecipient(
	                Message.RecipientType.TO, new InternetAddress(toAddress));
	        message.setSubject(subject);
	        message.setText(body);
	        Transport.send(message);

	    }
}
